package springboot.j2024_11_3;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RequestMapping("/session")
@RestController
public class SessionController {
     @RequestMapping("/set")
     public String set(HttpServletRequest request){
         HttpSession session = request.getSession();
         session.setAttribute("name" ,"zhangsan");
         return "设置Session成功";
     }

    @RequestMapping("/s1")
    public String s1(HttpServletRequest request){
        HttpSession session = request.getSession();
        String name = (String) session.getAttribute("name");
        return "name:" + name;
    }
    @RequestMapping("/s2")
    public String s2(HttpSession session) {
         String name = (String) session.getAttribute("name");
         return "name: "+ name;
    }

     @RequestMapping("/s3")
    public String s3(@SessionAttribute( value = "name" ,required = false) String name){
         return name;
     }

}
